import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'email_add_logic.dart';

class EmailAddDialog extends StatelessWidget {
  const EmailAddDialog({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final controller = Get.put(EmailDialogController());

    return CupertinoAlertDialog(
      title: Text("邮箱".tr, style: theme.textTheme.titleLarge),
      content: Padding(
        padding: const EdgeInsets.only(top: 16.0),
        child: Column(
          children: [
            CupertinoTextField(
              placeholder: "your@email.com",
              controller: controller.emailTextController,
              keyboardType: TextInputType.emailAddress,
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
              // 添加清除按钮
              suffix: Obx(() {
                return controller.email.isNotEmpty
                    ? CupertinoButton(
                        padding: const EdgeInsets.all(8),
                        minSize: 20,
                        child: const Icon(
                          Icons.clear,
                          size: 18,
                          color: CupertinoColors.placeholderText,
                        ),
                        onPressed: controller.clearEmail,
                      )
                    : SizedBox();
              }),
              onChanged: controller.validateEmail,
              suffixMode: OverlayVisibilityMode.editing,
              style: theme.textTheme.titleMedium,
            ),
            const SizedBox(height: 8),
            Obx(() => Text(
                  controller.emailError.value,
                  style: const TextStyle(color: Colors.red, fontSize: 12),
                )),
          ],
        ),
      ),
      actions: [
        CupertinoDialogAction(
          isDefaultAction: false,
          onPressed: () => Get.back(),
          child: Text("取消".tr, style: theme.textTheme.bodyMedium),
        ),
        Obx(
          () => CupertinoDialogAction(
            onPressed: controller.isOk.value ? controller.submit : null,
            child: Text(
              "确定".tr,
              style: theme.textTheme.bodyMedium?.copyWith(
                color: controller.isOk.value
                    ? theme.primaryColor
                    : CupertinoColors.inactiveGray,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
